<template>
  <div>
    <div class="mb-3">
      <el-button
        v-if="customerData.project_status === '0' || customerData.project_status === '2'"
        type="primary"
        @click="edit()"
      >
        新增已还贷款
      </el-button>
    </div>
    <loading-wrap>
      <el-table border :data="tableData">
        <el-table-column prop="shareholder_name" width="100px" label="姓名" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.shareholder_name || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="load_num" width="100px" label="贷款笔数" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.load_num || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="return_funds" width="120px" label="结清贷款总金额" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.return_funds || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="overdue_num" width="100px" label="逾期次数" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.overdue_num || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="overdue_max_funds" label="最高逾期金额" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.overdue_max_funds || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="overdue_max_funds_months"
          label="最高逾期金额的逾期时长"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.overdue_max_funds_months || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="overdue_max_months" label="最长逾期时间" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.overdue_max_months || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="overdue_max_months_months"
          label="最长逾期时间的逾期金额"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.overdue_max_months_months || '-' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80px" v-if="!onFile" align="center">
          <template slot-scope="scope">
            <el-button type="text" class="py-0 px-1" @click="edit(scope.row)">
              <i class="fa fa-edit"></i>
            </el-button>
            <el-button type="text" class="py-0 px-1 text-danger" @click="deleteData(scope.row)">
              <i class="fa fa-trash"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </loading-wrap>

    <form-component
      :visible.sync="editVisible"
      :table-name="tableName"
      ref="fc"
      :form-append="{}"
      :form-data="formData"
      :table-id="tableId"
      :table-data="editData"
      :auto-init="false"
      @success="getData"
    ></form-component>
  </div>
</template>
<script>
import FormComponent from '@/components/FormComponent'
import SelectPerson from '@/credit/components/select/SelectPerson'
export default {
  components: {
    FormComponent,
  },
  props: {},
  data() {
    return {
      editVisible: false,
      tableName: 'datum_customer_credit_return',
      formData: {
        datum_customer_shareholder_id: {
          type: 'component',
          label: '姓名',
          value: '',
          required: true,
          component: SelectPerson,
          attrs: { css: 'form-control-ml' },
          on: {
            getname: name => {
              const ref = this.$refs.fc
              ref.setKey('shareholder_name', 'value', name)
            },
          },
        },
        shareholder_name: {
          type: 'text',
          hide: true,
          value: '',
        },
        load_num: {
          type: 'number',
          label: '贷款笔数',
          value: '',
          step: 1,
          required: true,
        },
        return_funds: {
          type: 'number',
          label: '结清贷款总金额',
          value: '',
          required: true,
          unit: '万元',
        },
        overdue_num: {
          type: 'number',
          step: 1,
          label: '逾期次数',
          value: '',
          required: true,
        },
        overdue_max_funds: {
          type: 'number',
          label: '结清贷款最高逾期金额',
          value: '',
          required: true,
          unit: '万元',
          step: 0.0001,
        },
        overdue_max_funds_months: {
          type: 'number',
          step: 1,
          label: '结清贷款最高逾期金额的逾期时长',
          value: '',
          unit: '月',
          required: true,
        },
        overdue_max_months: {
          type: 'number',
          step: 1,
          label: '结清贷款最长逾期时间',
          value: '',
          unit: '月',
          required: true,
        },
        overdue_max_months_months: {
          type: 'number',
          label: '结清贷款最长逾期时间的逾期金额',
          value: '',
          required: true,
          unit: '万元',
        },
      },
      editData: {},
      tableId: '',
      tableData: [],
    }
  },
  computed: {
    onFile() {
      return this.customerData.project_status === '1' || this.customerData.project_status === '3'
    },
  },
  watch: {},
  created() {
    this.customerData = JSON.parse(sessionStorage.getItem('customer_related_view'))
    this.id = this.$route.query.id || sessionStorage.getItem('customer_related_id')
    this.getData()
  },
  mounted() {},
  methods: {
    getData() {
      this.$api
        .getData('credit', this.tableName, {
          'customer_related_id.eq': this.id,
        })
        .then(res => {
          this.tableData = (res && res.data) || []
        })
    },
    edit(p) {
      this.editVisible = true
      this.tableId = p ? p.id : ''
      this.editData = p
    },
    deleteData(item) {
      const h = this.$createElement
      this.$msgbox({
        title: '提示',
        message: h('span', undefined, `确定删除？`),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        beforeClose: async (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '删除中...'
            const [res] = await this.$utils.promise(
              this.$api.deleteData('credit', this.tableName, item.id)
            )
            instance.confirmButtonLoading = false
            instance.confirmButtonText = '确定'
            if (res) {
              this.$message.success('删除成功！')
              this.getData()
              done()
            } else {
              this.$message.error('删除失败，请重试！')
            }
          } else {
            done()
          }
        },
      }).catch(() => {})
    },
  },
}
</script>
<style scoped></style>
